{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# pandapower Optimal Power Flow\n",
    "This is an introduction into the usage of the pandapower optimal power flow. It shows how to set the constraints and the cost factors into the pandapower element tables.\n",
    "\n",
    "## Example Network\n",
    "\n",
    "We use the following four bus example network for this tutorial:\n",
    "\n",
    "<img src=\"pics/example_opf.png\" width=\"50%\">\n",
    "\n",
    "We first create this network in pandapower:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandapower as pp\n",
    "import numpy as np\n",
    "net = pp.create_empty_network()\n",
    "\n",
    "#create buses\n",
    "bus1 = pp.create_bus(net, vn_kv=220.)\n",
    "bus2 = pp.create_bus(net, vn_kv=110.)\n",
    "bus3 = pp.create_bus(net, vn_kv=110.)\n",
    "bus4 = pp.create_bus(net, vn_kv=110.)\n",
    "\n",
    "#create 220/110 kV transformer\n",
    "pp.create_transformer(net, bus1, bus2, std_type=\"100 MVA 220/110 kV\")\n",
    "\n",
    "#create 110 kV lines\n",
    "pp.create_line(net, bus2, bus3, length_km=70., std_type='149-AL1/24-ST1A 110.0')\n",
    "pp.create_line(net, bus3, bus4, length_km=50., std_type='149-AL1/24-ST1A 110.0')\n",
    "pp.create_line(net, bus4, bus2, length_km=40., std_type='149-AL1/24-ST1A 110.0')\n",
    "\n",
    "#create loads\n",
    "pp.create_load(net, bus2, p_mw=60, controllable=False)\n",
    "pp.create_load(net, bus3, p_mw=70, controllable=False)\n",
    "pp.create_load(net, bus4, p_mw=10, controllable=False)\n",
    "\n",
    "#create generators\n",
    "eg = pp.create_ext_grid(net, bus1, min_p_mw=-1000, max_p_mw=1000)\n",
    "g0 = pp.create_gen(net, bus3, p_mw=80, min_p_mw=0, max_p_mw=80,  vm_pu=1.01, controllable=True)\n",
    "g1 = pp.create_gen(net, bus4, p_mw=100, min_p_mw=0, max_p_mw=100, vm_pu=1.01, controllable=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loss Minimization\n",
    "\n",
    "We specify the same costs for the power at the external grid and all generators to minimize the overall power feed in. This equals an overall loss minimization:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "costeg = pp.create_poly_cost(net, 0, 'ext_grid', cp1_eur_per_mw=10)\n",
    "costgen1 = pp.create_poly_cost(net, 0, 'gen', cp1_eur_per_mw=10)\n",
    "costgen2 = pp.create_poly_cost(net, 1, 'gen', cp1_eur_per_mw=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n",
      "hp.pandapower.run - INFO: max_vm_pu is missing in bus table. In OPF these limits are considered as 2.0 pu.\n"
     ]
    }
   ],
   "source": [
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "let's check the results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>56.530584</td>\n",
       "      <td>1.974564</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar\n",
       "0  56.530584  1.974564"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>71.309255</td>\n",
       "      <td>-1.969681</td>\n",
       "      <td>-3.713031</td>\n",
       "      <td>1.000008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>12.303443</td>\n",
       "      <td>-1.451180</td>\n",
       "      <td>-3.712735</td>\n",
       "      <td>1.000010</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar  va_degree     vm_pu\n",
       "0  71.309255 -1.969681  -3.713031  1.000008\n",
       "1  12.303443 -1.451180  -3.712735  1.000010"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since all costs were specified the same, the OPF minimizes overall power generation, which is equal to a loss minimization in the network. The loads at buses 3 and 4 are supplied by generators at the same bus, the load at Bus 2 is provided by a combination of the other generators so that the power transmission leads to minimal losses."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Individual Generator Costs\n",
    "\n",
    "Let's now assign individual costs to each generator.\n",
    "\n",
    "We assign a cost of 10 ct/kW for the external grid, 15 ct/kw for the generator g0 and 12 ct/kw for generator g1:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.poly_cost.cp1_eur_per_mw.at[costeg] = 10\n",
    "net.poly_cost.cp1_eur_per_mw.at[costgen1] = 15\n",
    "net.poly_cost.cp1_eur_per_mw.at[costgen2] = 12"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And now run an OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n",
      "hp.pandapower.run - INFO: max_vm_pu is missing in bus table. In OPF these limits are considered as 2.0 pu.\n"
     ]
    }
   ],
   "source": [
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that all active power is provided by the external grid: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>144.559166</td>\n",
       "      <td>9.193021</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         p_mw    q_mvar\n",
       "0  144.559166  9.193021"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000079</td>\n",
       "      <td>8.601766</td>\n",
       "      <td>-16.426835</td>\n",
       "      <td>0.967619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000225</td>\n",
       "      <td>10.594623</td>\n",
       "      <td>-13.481007</td>\n",
       "      <td>0.989756</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       p_mw     q_mvar  va_degree     vm_pu\n",
       "0  0.000079   8.601766 -16.426835  0.967619\n",
       "1  0.000225  10.594623 -13.481007  0.989756"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This makes sense, because the external grid has the lowest cost of all generators and we did not define any constraints.\n",
    "\n",
    "The dispatch costs are given in net.res_cost:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1445.5955448589666"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformer Constraint\n",
    "\n",
    "Since all active power comes from the external grid and subsequently flows through the transformer, the transformer is overloaded with a loading of about 145%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    144.851179\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_trafo.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We now limit the transformer loading to 50%:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.trafo[\"max_loading_percent\"] = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(the max_loading_percent parameter can also be specified directly when creating the transformer)\n",
    "and run the OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n",
      "hp.pandapower.run - INFO: max_vm_pu is missing in bus table. In OPF these limits are considered as 2.0 pu.\n"
     ]
    }
   ],
   "source": [
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that the transformer complies with the maximum loading:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    49.999136\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_trafo.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And power generation is now split between the external grid and generator 1 (which is the second cheapest generation unit):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>49.953012</td>\n",
       "      <td>-2.147126</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar\n",
       "0  49.953012 -2.147126"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.000598</td>\n",
       "      <td>2.992989</td>\n",
       "      <td>-6.232710</td>\n",
       "      <td>0.985230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>93.304317</td>\n",
       "      <td>3.453173</td>\n",
       "      <td>-1.237784</td>\n",
       "      <td>1.025709</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar  va_degree     vm_pu\n",
       "0   0.000598  2.992989  -6.232710  0.985230\n",
       "1  93.304317  3.453173  -1.237784  1.025709"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This comes of course with an increase in dispatch costs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1619.1908981408608"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Line Loading Constraints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wen now look at the line loadings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    22.192863\n",
       "1    57.476322\n",
       "2    33.473473\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and run the OPF with a 50% loading constraint:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n",
      "hp.pandapower.run - INFO: min_vm_pu is missing in bus table. In OPF these limits are considered as 0.0 pu.\n",
      "hp.pandapower.run - INFO: max_vm_pu is missing in bus table. In OPF these limits are considered as 2.0 pu.\n"
     ]
    }
   ],
   "source": [
    "net.line[\"max_loading_percent\"] = 50\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the line loading constraint is complied with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    18.905589\n",
       "1    49.999986\n",
       "2    30.435895\n",
       "Name: loading_percent, dtype: float64"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line.loading_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And all generators are involved in supplying the loads:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>49.787584</td>\n",
       "      <td>-4.603451</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar\n",
       "0  49.787584 -4.603451"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9.136446</td>\n",
       "      <td>2.430957</td>\n",
       "      <td>-5.815440</td>\n",
       "      <td>0.993015</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>83.592614</td>\n",
       "      <td>4.853496</td>\n",
       "      <td>-1.511326</td>\n",
       "      <td>1.028887</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw    q_mvar  va_degree     vm_pu\n",
       "0   9.136446  2.430957  -5.815440  0.993015\n",
       "1  83.592614  4.853496  -1.511326  1.028887"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This of course comes with a once again rising dispatch cost:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1638.0339026783777"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Voltage Constraints\n",
    "\n",
    "Finally, we have a look at the bus voltage:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vm_pu</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>lam_p</th>\n",
       "      <th>lam_q</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-49.787584</td>\n",
       "      <td>4.603451</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>-1.410210e-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.006024</td>\n",
       "      <td>-3.408832</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>13.095255</td>\n",
       "      <td>-5.409162e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.993015</td>\n",
       "      <td>-5.815440</td>\n",
       "      <td>60.863554</td>\n",
       "      <td>-2.430957</td>\n",
       "      <td>14.999985</td>\n",
       "      <td>4.490946e-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.028887</td>\n",
       "      <td>-1.511326</td>\n",
       "      <td>-73.592614</td>\n",
       "      <td>-4.853496</td>\n",
       "      <td>12.000007</td>\n",
       "      <td>1.781412e-21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      vm_pu  va_degree       p_mw    q_mvar      lam_p         lam_q\n",
       "0  1.000000   0.000000 -49.787584  4.603451  10.000000 -1.410210e-21\n",
       "1  1.006024  -3.408832  60.000000  0.000000  13.095255 -5.409162e-02\n",
       "2  0.993015  -5.815440  60.863554 -2.430957  14.999985  4.490946e-22\n",
       "3  1.028887  -1.511326 -73.592614 -4.853496  12.000007  1.781412e-21"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and constrain it:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n"
     ]
    }
   ],
   "source": [
    "net.bus[\"min_vm_pu\"] = 1.0\n",
    "net.bus[\"max_vm_pu\"] = 1.02\n",
    "pp.runopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see that all voltages are within the voltage band:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vm_pu</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>lam_p</th>\n",
       "      <th>lam_q</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-49.906839</td>\n",
       "      <td>3.050610</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>-2.360898e-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.004168</td>\n",
       "      <td>-3.421014</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>13.126862</td>\n",
       "      <td>-2.133418e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-5.976093</td>\n",
       "      <td>59.278168</td>\n",
       "      <td>-14.859011</td>\n",
       "      <td>14.999997</td>\n",
       "      <td>1.148963e-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.020000</td>\n",
       "      <td>-1.366891</td>\n",
       "      <td>-71.863462</td>\n",
       "      <td>9.172781</td>\n",
       "      <td>12.000002</td>\n",
       "      <td>-7.089262e-22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      vm_pu  va_degree       p_mw     q_mvar      lam_p         lam_q\n",
       "0  1.000000   0.000000 -49.906839   3.050610  10.000000 -2.360898e-22\n",
       "1  1.004168  -3.421014  60.000000   0.000000  13.126862 -2.133418e-02\n",
       "2  1.000000  -5.976093  59.278168 -14.859011  14.999997  1.148963e-21\n",
       "3  1.020000  -1.366891 -71.863462   9.172781  12.000002 -7.089262e-22"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And all generators are once again involved in supplying the loads:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>49.906839</td>\n",
       "      <td>-3.05061</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw   q_mvar\n",
       "0  49.906839 -3.05061"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_ext_grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.721832</td>\n",
       "      <td>14.859011</td>\n",
       "      <td>-5.976093</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>81.863462</td>\n",
       "      <td>-9.172781</td>\n",
       "      <td>-1.366891</td>\n",
       "      <td>1.02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw     q_mvar  va_degree  vm_pu\n",
       "0  10.721832  14.859011  -5.976093   1.00\n",
       "1  81.863462  -9.172781  -1.366891   1.02"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This of course comes once again with rising dispatch costs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1642.2574101559044"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DC OPF\n",
    "\n",
    "pandapower also provides the possibility of running a DC Optimal Power Flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp.rundcopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since voltage magnitudes are not included in the DC power flow formulation, voltage constraints cannot be considered in the DC OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>vm_pu</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>lam_p</th>\n",
       "      <th>lam_q</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>2.369698e-23</td>\n",
       "      <td>-50.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-3.436967e+00</td>\n",
       "      <td>60.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>13.090909</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-5.708566e+00</td>\n",
       "      <td>61.488746</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.362340e+00</td>\n",
       "      <td>-71.488747</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   vm_pu     va_degree       p_mw  q_mvar      lam_p  lam_q\n",
       "0    NaN  2.369698e-23 -50.000000     NaN  10.000000    0.0\n",
       "1    NaN -3.436967e+00  60.000000     NaN  13.090909    0.0\n",
       "2    NaN -5.708566e+00  61.488746     NaN  15.000000    0.0\n",
       "3    NaN -1.362340e+00 -71.488747     NaN  12.000000    0.0"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Line and transformer loading limits are however complied with:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_from_mw</th>\n",
       "      <th>q_from_mvar</th>\n",
       "      <th>p_to_mw</th>\n",
       "      <th>q_to_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>ql_mvar</th>\n",
       "      <th>i_from_ka</th>\n",
       "      <th>i_to_ka</th>\n",
       "      <th>i_ka</th>\n",
       "      <th>vm_from_pu</th>\n",
       "      <th>va_from_degree</th>\n",
       "      <th>vm_to_pu</th>\n",
       "      <th>va_to_degree</th>\n",
       "      <th>loading_percent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16.715233</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-16.715233</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.087732</td>\n",
       "      <td>0.087732</td>\n",
       "      <td>0.087732</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-3.436967</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-5.708566</td>\n",
       "      <td>18.666430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-44.773513</td>\n",
       "      <td>0.0</td>\n",
       "      <td>44.773513</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.235000</td>\n",
       "      <td>0.235000</td>\n",
       "      <td>0.235000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-5.708566</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.362340</td>\n",
       "      <td>50.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>26.715233</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-26.715233</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.140219</td>\n",
       "      <td>0.140219</td>\n",
       "      <td>0.140219</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.362340</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-3.436967</td>\n",
       "      <td>29.833747</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   p_from_mw  q_from_mvar    p_to_mw  q_to_mvar  pl_mw  ql_mvar  i_from_ka  \\\n",
       "0  16.715233          0.0 -16.715233        0.0    0.0      0.0   0.087732   \n",
       "1 -44.773513          0.0  44.773513        0.0    0.0      0.0   0.235000   \n",
       "2  26.715233          0.0 -26.715233        0.0    0.0      0.0   0.140219   \n",
       "\n",
       "    i_to_ka      i_ka  vm_from_pu  va_from_degree  vm_to_pu  va_to_degree  \\\n",
       "0  0.087732  0.087732         1.0       -3.436967       1.0     -5.708566   \n",
       "1  0.235000  0.235000         1.0       -5.708566       1.0     -1.362340   \n",
       "2  0.140219  0.140219         1.0       -1.362340       1.0     -3.436967   \n",
       "\n",
       "   loading_percent  \n",
       "0        18.666430  \n",
       "1        50.000000  \n",
       "2        29.833747  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_hv_mw</th>\n",
       "      <th>q_hv_mvar</th>\n",
       "      <th>p_lv_mw</th>\n",
       "      <th>q_lv_mvar</th>\n",
       "      <th>pl_mw</th>\n",
       "      <th>ql_mvar</th>\n",
       "      <th>i_hv_ka</th>\n",
       "      <th>i_lv_ka</th>\n",
       "      <th>vm_hv_pu</th>\n",
       "      <th>va_hv_degree</th>\n",
       "      <th>vm_lv_pu</th>\n",
       "      <th>va_lv_degree</th>\n",
       "      <th>loading_percent</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>50.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-50.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.131216</td>\n",
       "      <td>0.262432</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.369698e-23</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-3.436967</td>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   p_hv_mw  q_hv_mvar  p_lv_mw  q_lv_mvar  pl_mw  ql_mvar   i_hv_ka   i_lv_ka  \\\n",
       "0     50.0        0.0    -50.0        0.0    0.0      0.0  0.131216  0.262432   \n",
       "\n",
       "   vm_hv_pu  va_hv_degree  vm_lv_pu  va_lv_degree  loading_percent  \n",
       "0       1.0  2.369698e-23       1.0     -3.436967             50.0  "
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_trafo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As are generator limits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>bus</th>\n",
       "      <th>p_mw</th>\n",
       "      <th>vm_pu</th>\n",
       "      <th>sn_mva</th>\n",
       "      <th>min_q_mvar</th>\n",
       "      <th>max_q_mvar</th>\n",
       "      <th>scaling</th>\n",
       "      <th>slack</th>\n",
       "      <th>in_service</th>\n",
       "      <th>type</th>\n",
       "      <th>min_p_mw</th>\n",
       "      <th>max_p_mw</th>\n",
       "      <th>controllable</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>None</td>\n",
       "      <td>2</td>\n",
       "      <td>80.0</td>\n",
       "      <td>1.01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>None</td>\n",
       "      <td>0.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>None</td>\n",
       "      <td>3</td>\n",
       "      <td>100.0</td>\n",
       "      <td>1.01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>False</td>\n",
       "      <td>True</td>\n",
       "      <td>None</td>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   name  bus   p_mw  vm_pu  sn_mva  min_q_mvar  max_q_mvar  scaling  slack  \\\n",
       "0  None    2   80.0   1.01     NaN         NaN         NaN      1.0  False   \n",
       "1  None    3  100.0   1.01     NaN         NaN         NaN      1.0  False   \n",
       "\n",
       "   in_service  type  min_p_mw  max_p_mw  controllable  \n",
       "0        True  None       0.0      80.0          True  \n",
       "1        True  None       0.0     100.0          True  "
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8.511254</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-5.708566</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>81.488747</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.362340</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw  q_mvar  va_degree  vm_pu\n",
       "0   8.511254     NaN  -5.708566    1.0\n",
       "1  81.488747     NaN  -1.362340    1.0"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The cost function is the same for the linearized OPF as for the non-linear one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1605.533761110142"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Piecewise linear cost functions\n",
    "\n",
    "The OPF also offers piecewise linear cost functions. Let us first check the actual cost function setup:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>element</th>\n",
       "      <th>et</th>\n",
       "      <th>cp0_eur</th>\n",
       "      <th>cp1_eur_per_mw</th>\n",
       "      <th>cp2_eur_per_mw2</th>\n",
       "      <th>cq0_eur</th>\n",
       "      <th>cq1_eur_per_mvar</th>\n",
       "      <th>cq2_eur_per_mvar2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>ext_grid</td>\n",
       "      <td>0.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>gen</td>\n",
       "      <td>0.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>gen</td>\n",
       "      <td>0.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   element        et  cp0_eur  cp1_eur_per_mw  cp2_eur_per_mw2  cq0_eur  \\\n",
       "0      0.0  ext_grid      0.0            10.0              0.0      0.0   \n",
       "1      0.0       gen      0.0            15.0              0.0      0.0   \n",
       "2      1.0       gen      0.0            12.0              0.0      0.0   \n",
       "\n",
       "   cq1_eur_per_mvar  cq2_eur_per_mvar2  \n",
       "0               0.0                0.0  \n",
       "1               0.0                0.0  \n",
       "2               0.0                0.0  "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.poly_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An element can either have polynomial costs or piecewise linear costs at the same time. So let us first delete the polynomial costs in order to avoid confusion and errors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.poly_cost.drop(net.poly_cost.index.values, inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results above have been produced with linear polynomial cost functions. Let's try to reproduce the results using piecewise linear cost functions. Costs have to be defined for the whole range of generators and external grids:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min_p_mw</th>\n",
       "      <th>max_p_mw</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>80.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   min_p_mw  max_p_mw\n",
       "0       0.0      80.0\n",
       "1       0.0     100.0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.gen[[\"min_p_mw\", \"max_p_mw\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min_p_mw</th>\n",
       "      <th>max_p_mw</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1000.0</td>\n",
       "      <td>1000.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   min_p_mw  max_p_mw\n",
       "0   -1000.0    1000.0"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.ext_grid[[\"min_p_mw\", \"max_p_mw\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We define the piecewise linear cost as constant over the whole range to reproduce the polyomial costs defined above:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pp.create_pwl_cost(net, 0, \"gen\", [[0, 80, 15]])\n",
    "pp.create_pwl_cost(net, 1, \"gen\", [[0, 100, 12]])\n",
    "pp.create_pwl_cost(net, 0, \"ext_grid\", [[-1000, 1000, 10]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let us check the results from the previous OPF again!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8.511254</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-5.708566</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>81.488747</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.362340</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw  q_mvar  va_degree  vm_pu\n",
       "0   8.511254     NaN  -5.708566    1.0\n",
       "1  81.488747     NaN  -1.362340    1.0"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1605.533761110142"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run the same OPF now with different cost function setup. We should get the exact same results:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp.rundcopp(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8.511254</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-5.708566</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>81.488747</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.362340</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw  q_mvar  va_degree  vm_pu\n",
       "0   8.511254     NaN  -5.708566    1.0\n",
       "1  81.488747     NaN  -1.362340    1.0"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1605.533761110142"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_cost"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now lets define real piecewise linear costs for generator 1. We define the costs as 12€/MW up to 70MW and 20€/MW from 70MW to 100MW:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "net.pwl_cost.points.loc[1] = [[0, 70, 12], [70, 100, 20]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And run another OPF:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "pp.rundcopp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can see that generator 1 only dispatches 70MW, above which generator 0 becomes less expensive and is therefore dispatched:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>p_mw</th>\n",
       "      <th>q_mvar</th>\n",
       "      <th>va_degree</th>\n",
       "      <th>vm_pu</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20.000007</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-5.220652</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>69.999997</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-1.641146</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        p_mw  q_mvar  va_degree  vm_pu\n",
       "0  20.000007     NaN  -5.220652    1.0\n",
       "1  69.999997     NaN  -1.641146    1.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.res_gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Debugging\n",
    "\n",
    "For more information on the status of the OPF solver, set verbose=True:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "hp.pandapower.run - INFO: These elements have missing power constraint values, which are considered in OPF as +- 1000 TW: ['gen']\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PYPOWER Version 5.1.4, 27-June-2018 -- AC Optimal Power Flow\n",
      "Python Interior Point Solver - PIPS, Version 1.0, 07-Feb-2011\n",
      "Converged!\n",
      "\n",
      "Converged in 1.19 seconds\n",
      "================================================================================\n",
      "|     System Summary                                                           |\n",
      "================================================================================\n",
      "\n",
      "How many?                How much?              P (MW)            Q (MVAr)\n",
      "---------------------    -------------------  -------------  -----------------\n",
      "Buses              4     Total Gen Capacity    1180.0       -3000000000.0 to 3000000000.0\n",
      "Generators         3     On-line Capacity      1180.0       -3000000000.0 to 3000000000.0\n",
      "Committed Gens     3     Generation (actual)    141.7               1.0\n",
      "Loads              3     Load                   140.0               0.0\n",
      "  Fixed            3       Fixed                140.0               0.0\n",
      "  Dispatchable     0       Dispatchable           0.0 of 0.0        0.0\n",
      "Shunts             0     Shunt (inj)              0.0               0.0\n",
      "Branches           4     Losses (I^2 * Z)         1.65              6.34\n",
      "Transformers       4     Branch Charging (inj)     -                5.4\n",
      "Inter-ties         0     Total Inter-tie Flow     0.0               0.0\n",
      "Areas              1\n",
      "\n",
      "                          Minimum                      Maximum\n",
      "                 -------------------------  --------------------------------\n",
      "Voltage Magnitude   1.000 p.u. @ bus 0          1.020 p.u. @ bus 3   \n",
      "Voltage Angle      -5.39 deg   @ bus 2          0.00 deg   @ bus 0   \n",
      "P Losses (I^2*R)             -                  1.05 MW    @ line 2-3\n",
      "Q Losses (I^2*X)             -                  3.00 MVAr  @ line 0-1\n",
      "\n",
      "================================================================================\n",
      "|     Area Summary                                                             |\n",
      "================================================================================\n",
      "Area  # of      # of Gens        # of Loads         # of    # of   # of   # of\n",
      " Num  Buses   Total  Online   Total  Fixed  Disp    Shunt   Brchs  Xfmrs   Ties\n",
      "----  -----   -----  ------   -----  -----  -----   -----   -----  -----  -----\n",
      "  1       4       3      3       3      3      0       0       4      4      0\n",
      "----  -----   -----  ------   -----  -----  -----   -----   -----  -----  -----\n",
      "Tot:      4       3      3       3      3      0       0       4      4      0\n",
      "\n",
      "Area      Total Gen Capacity           On-line Gen Capacity         Generation\n",
      " Num     MW           MVAr            MW           MVAr             MW    MVAr\n",
      "----   ------  ------------------   ------  ------------------    ------  ------\n",
      "  1    1180.0  -3000000000.0 to 3000000000.0   1180.0  -3000000000.0 to 3000000000.0     141.7     1.0\n",
      "----   ------  ------------------   ------  ------------------    ------  ------\n",
      "\n",
      "Area    Disp Load Cap       Disp Load         Fixed Load        Total Load\n",
      " Num      MW     MVAr       MW     MVAr       MW     MVAr       MW     MVAr\n",
      "----    ------  ------    ------  ------    ------  ------    ------  ------\n",
      "  1        0.0     0.0       0.0     0.0     140.0     0.0     140.0     0.0\n",
      "----    ------  ------    ------  ------    ------  ------    ------  ------\n",
      "Tot:       0.0     0.0       0.0     0.0     140.0     0.0     140.0     0.0\n",
      "\n",
      "Area      Shunt Inj        Branch      Series Losses      Net Export\n",
      " Num      MW     MVAr     Charging      MW     MVAr       MW     MVAr\n",
      "----    ------  ------    --------    ------  ------    ------  ------\n",
      "  1        0.0     0.0        5.4       1.65    6.34       0.0     0.0\n",
      "----    ------  ------    --------    ------  ------    ------  ------\n",
      "Tot:       0.0     0.0        5.4       1.65    6.34       -       -\n",
      "\n",
      "================================================================================\n",
      "|     Generator Data                                                           |\n",
      "================================================================================\n",
      " Gen   Bus   Status     Pg        Qg   \n",
      "  #     #              (MW)     (MVAr) \n",
      "----  -----  ------  --------  --------\n",
      "  0      0      1      49.90     -3.17\n",
      "  1      2      1      21.81      8.70\n",
      "  2      3      1      70.00     -4.56\n",
      "                     --------  --------\n",
      "            Total:    141.70      0.97\n",
      "\n",
      "================================================================================\n",
      "|     Bus Data                                                                 |\n",
      "================================================================================\n",
      " Bus      Voltage          Generation             Load        \n",
      "  #   Mag(pu) Ang(deg)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)\n",
      "----- ------- --------  --------  --------  --------  --------\n",
      "    0  1.000    0.000*    49.90     -3.17       -         -   \n",
      "    1  1.004   -3.420       -         -       60.00      0.00 \n",
      "    2  1.000   -5.387     21.81      8.70     70.00      0.00 \n",
      "    3  1.020   -1.699     70.00     -4.56     10.00      0.00 \n",
      "                        --------  --------  --------  --------\n",
      "               Total:    141.70      0.97    140.00      0.00\n",
      "\n",
      "================================================================================\n",
      "|     Branch Data                                                              |\n",
      "================================================================================\n",
      "Brnch   From   To    From Bus Injection   To Bus Injection     Loss (I^2 * Z)  \n",
      "  #     Bus    Bus    P (MW)   Q (MVAr)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)\n",
      "-----  -----  -----  --------  --------  --------  --------  --------  --------\n",
      "   0      1      2     12.68     -5.10    -12.48      3.17     0.196      0.41\n",
      "   1      2      3    -35.71      5.53     36.77     -5.00     1.055      2.23\n",
      "   2      3      1     23.23      0.44    -22.90     -1.09     0.333      0.70\n",
      "   3      0      1     49.90     -3.17    -49.78      6.19     0.065      3.00\n",
      "                                                             --------  --------\n",
      "                                                    Total:     1.649      6.34\n",
      "\n",
      "Converged in 1.19 seconds\n",
      "================================================================================\n",
      "|     System Summary                                                           |\n",
      "================================================================================\n",
      "\n",
      "How many?                How much?              P (MW)            Q (MVAr)\n",
      "---------------------    -------------------  -------------  -----------------\n",
      "Buses              4     Total Gen Capacity    1180.0       -3000000000.0 to 3000000000.0\n",
      "Generators         3     On-line Capacity      1180.0       -3000000000.0 to 3000000000.0\n",
      "Committed Gens     3     Generation (actual)    141.7               1.0\n",
      "Loads              3     Load                   140.0               0.0\n",
      "  Fixed            3       Fixed                140.0               0.0\n",
      "  Dispatchable     0       Dispatchable           0.0 of 0.0        0.0\n",
      "Shunts             0     Shunt (inj)              0.0               0.0\n",
      "Branches           4     Losses (I^2 * Z)         1.65              6.34\n",
      "Transformers       4     Branch Charging (inj)     -                5.4\n",
      "Inter-ties         0     Total Inter-tie Flow     0.0               0.0\n",
      "Areas              1\n",
      "\n",
      "                          Minimum                      Maximum\n",
      "                 -------------------------  --------------------------------\n",
      "Voltage Magnitude   1.000 p.u. @ bus 0          1.020 p.u. @ bus 3   \n",
      "Voltage Angle      -5.39 deg   @ bus 2          0.00 deg   @ bus 0   \n",
      "P Losses (I^2*R)             -                  1.05 MW    @ line 2-3\n",
      "Q Losses (I^2*X)             -                  3.00 MVAr  @ line 0-1\n",
      "\n",
      "================================================================================\n",
      "|     Area Summary                                                             |\n",
      "================================================================================\n",
      "Area  # of      # of Gens        # of Loads         # of    # of   # of   # of\n",
      " Num  Buses   Total  Online   Total  Fixed  Disp    Shunt   Brchs  Xfmrs   Ties\n",
      "----  -----   -----  ------   -----  -----  -----   -----   -----  -----  -----\n",
      "  1       4       3      3       3      3      0       0       4      4      0\n",
      "----  -----   -----  ------   -----  -----  -----   -----   -----  -----  -----\n",
      "Tot:      4       3      3       3      3      0       0       4      4      0\n",
      "\n",
      "Area      Total Gen Capacity           On-line Gen Capacity         Generation\n",
      " Num     MW           MVAr            MW           MVAr             MW    MVAr\n",
      "----   ------  ------------------   ------  ------------------    ------  ------\n",
      "  1    1180.0  -3000000000.0 to 3000000000.0   1180.0  -3000000000.0 to 3000000000.0     141.7     1.0\n",
      "----   ------  ------------------   ------  ------------------    ------  ------\n",
      "\n",
      "Area    Disp Load Cap       Disp Load         Fixed Load        Total Load\n",
      " Num      MW     MVAr       MW     MVAr       MW     MVAr       MW     MVAr\n",
      "----    ------  ------    ------  ------    ------  ------    ------  ------\n",
      "  1        0.0     0.0       0.0     0.0     140.0     0.0     140.0     0.0\n",
      "----    ------  ------    ------  ------    ------  ------    ------  ------\n",
      "Tot:       0.0     0.0       0.0     0.0     140.0     0.0     140.0     0.0\n",
      "\n",
      "Area      Shunt Inj        Branch      Series Losses      Net Export\n",
      " Num      MW     MVAr     Charging      MW     MVAr       MW     MVAr\n",
      "----    ------  ------    --------    ------  ------    ------  ------\n",
      "  1        0.0     0.0        5.4       1.65    6.34       0.0     0.0\n",
      "----    ------  ------    --------    ------  ------    ------  ------\n",
      "Tot:       0.0     0.0        5.4       1.65    6.34       -       -\n",
      "\n",
      "================================================================================\n",
      "|     Generator Data                                                           |\n",
      "================================================================================\n",
      " Gen   Bus   Status     Pg        Qg   \n",
      "  #     #              (MW)     (MVAr) \n",
      "----  -----  ------  --------  --------\n",
      "  0      0      1      49.90     -3.17\n",
      "  1      2      1      21.81      8.70\n",
      "  2      3      1      70.00     -4.56\n",
      "                     --------  --------\n",
      "            Total:    141.70      0.97\n",
      "\n",
      "================================================================================\n",
      "|     Bus Data                                                                 |\n",
      "================================================================================\n",
      " Bus      Voltage          Generation             Load        \n",
      "  #   Mag(pu) Ang(deg)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)\n",
      "----- ------- --------  --------  --------  --------  --------\n",
      "    0  1.000    0.000*    49.90     -3.17       -         -   \n",
      "    1  1.004   -3.420       -         -       60.00      0.00 \n",
      "    2  1.000   -5.387     21.81      8.70     70.00      0.00 \n",
      "    3  1.020   -1.699     70.00     -4.56     10.00      0.00 \n",
      "                        --------  --------  --------  --------\n",
      "               Total:    141.70      0.97    140.00      0.00\n",
      "\n",
      "================================================================================\n",
      "|     Branch Data                                                              |\n",
      "================================================================================\n",
      "Brnch   From   To    From Bus Injection   To Bus Injection     Loss (I^2 * Z)  \n",
      "  #     Bus    Bus    P (MW)   Q (MVAr)   P (MW)   Q (MVAr)   P (MW)   Q (MVAr)\n",
      "-----  -----  -----  --------  --------  --------  --------  --------  --------\n",
      "   0      1      2     12.68     -5.10    -12.48      3.17     0.196      0.41\n",
      "   1      2      3    -35.71      5.53     36.77     -5.00     1.055      2.23\n",
      "   2      3      1     23.23      0.44    -22.90     -1.09     0.333      0.70\n",
      "   3      0      1     49.90     -3.17    -49.78      6.19     0.065      3.00\n",
      "                                                             --------  --------\n",
      "                                                    Total:     1.649      6.34\n"
     ]
    }
   ],
   "source": [
    "pp.runopp(net, verbose=True)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "celltoolbar": "Raw Cell Format",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
